// GeneralJointRegular.cpp: implementation of the GeneralJointRegular class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GeneralJointRegular.h"
#include "jointstate.h"
#include "FirstFixNextUnfix.h"
#include "AllFix.h"
#include "Joint.h"
#include "SupportHanger.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_CREATE(GeneralJointRegular, JointRegular, 0)

GeneralJointRegular::GeneralJointRegular()
{
	this->m_pFirstFormer = NULL;
	this->m_pNextFormer = NULL;
	m_nFirstJointStyleID = 0;
	m_nNextJointStyleID = 0;
}

GeneralJointRegular::~GeneralJointRegular()
{

}

BOOL GeneralJointRegular::HasJointPort(JointSystem::JointPortFormer *pFormer)
{
	if(pFormer == this->m_pFirstFormer || pFormer == this->m_pNextFormer)
		return TRUE;
	return FALSE;
}

BOOL GeneralJointRegular::Execute()
{
	GenerateJointState();
	return GetJointState()->Execute();

}

void GeneralJointRegular::GenerateJointState()
{
	static FirstFixNextUnfix FirstFixNextUnfixState;
	static AllFix AllFixState;
	JointRegular::GenerateJointState(FirstFixNextUnfixState, AllFixState);
}

GeneralJointRegular* GeneralJointRegular::Clone()
{
	GeneralJointRegular* pRegular = new GeneralJointRegular;
	
	pRegular->m_nFirstJointStyleID = m_nFirstJointStyleID;
	pRegular->m_nNextJointStyleID = m_nNextJointStyleID;
	pRegular->m_strNextMatrixRegular = m_strNextMatrixRegular;
	pRegular->m_pFirstFormer = m_pFirstFormer;
	pRegular->m_pNextFormer = m_pNextFormer;
	pRegular->m_strPrevMatrixRegular = m_strPrevMatrixRegular;

	return pRegular;
}

void GeneralJointRegular::CaculateMatrix()
{
	JointFunction* pPrevFunction = GetJoint()->GetPrev();
	JointFunction* pNextFunction = GetJoint()->GetNext();

	GetFreeField(this->m_strPrevMatrixRegular, pPrevFunction->freeField());
	GetFreeField(this->m_strNextMatrixRegular, pNextFunction->freeField());
/*
	Part* pPart = GetJoint()->GetPrev()->GetOwner();
	Part* pNextPart = GetJoint()->GetNext()->GetOwner();
	ASSERT(pPart);
	ASSERT(pNextPart);

	AcGeMatrix3d csSH = pPart->SupportHangerOwner()->ModelMatrix();
	AcGeMatrix3d csPrev = pPrevFunction->CoordinateInOwner()
		*pPart->ModelMatrix();
	AcGeMatrix3d csNext = pNextFunction->CoordinateInOwner()
		*pNextPart->ModelMatrix();

	FreeField field;
	JointInfo info(pNextPart);
	field = info.GetFreeField(pNextFunction);

	AcGeMatrix3d csTran;
	csTran.setToIdentity();
	CoordinatePoint(csTran, GetCoordInTotherCS(csPrev, csNext, AcGePoint3d(0,0,0)));

	pNextPart->ModelMatrix(pNextPart->ModelMatrix()*csTran);*/
}

void GeneralJointRegular::GetFreeField(CString& strField, FreeField& field)
{
	strField.MakeUpper();
	CString strTemp;
	TCHAR* pBuf = strTemp.GetBuffer(strField.GetLength() + 1);
	TCHAR* p = strField.GetBuffer(0);

	INT nIndex = 0;
	INT nCount = 0;
	while(true)
	{
		TCHAR c = p[nIndex];
		if(c == ',' || c == '\0')
		{
			pBuf[nCount] = '\0';
			if(nCount != 0)
			{
				CString str(pBuf);
				if(str == _T("X"))
				{
					field.SetXUnFree();
				}
				else if(str == _T("Y"))
				{
					field.SetYUnFree();
				}
				else if(str == _T("Z"))
				{
					field.SetZUnFree();
				}
				else if(str == _T("VX"))
				{
					field.SetvXUnFree();
				}
				else if(str == _T("VY"))
				{
					field.SetvYUnFree();
				}
				else if(str == _T("VZ"))
				{
					field.SetvZUnFree();
				}
				else
				{
					ASSERT(FALSE);
				}
			}
			if(c == '\0')
				break;
			nCount = 0;
			nIndex++;
			continue;
		}
		else
		{
			pBuf[nCount] = c;
		}
		nCount++;
		nIndex++;
	}
}